package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.ShopDealer;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.ShopDealerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@AllArgsConstructor
@RequestMapping("/shop/shopDealer")
@Tag(name = "店铺经销商信息接口")
public class ShopDealerController {

    private final ShopDealerService shopDealerService;


    @GetMapping("/page")
    @Operation(summary =  "分页获取店铺经销商列表", description = "分页获取店铺经销商列表")
    public ServerResponseEntity<IPage<ShopDealer>> page(@ParameterObject ShopDealer shopDealer, PageParam<ShopDealer> page){
        Long shopId = SecurityUtils.getShopUser().getShopId();
        shopDealer.setShopId(shopId);
        IPage<ShopDealer> shopDealerPage = shopDealerService.getShopDealerPage(page, shopDealer);
        return ServerResponseEntity.success(shopDealerPage);
    }

    @GetMapping("/list")
    @Operation(summary =  "获取店铺经销商列表", description = "获取店铺经销商列表")
    public ServerResponseEntity<List<ShopDealer>> getShopDealerList(@ParameterObject ShopDealer shopDealer){
        Long shopId = SecurityUtils.getShopUser().getShopId();
        List<ShopDealer> shopDealerPage = shopDealerService.list(new LambdaQueryWrapper<ShopDealer>().eq(ShopDealer::getShopId, shopId).eq(ShopDealer::getStatus, shopDealer.getStatus()).orderByAsc(ShopDealer::getCreateTime));
        return ServerResponseEntity.success(shopDealerPage);
    }


    @PostMapping
    @Operation(summary = "新增店铺经销商信息", description = "新增店铺经销商信息")
    public ServerResponseEntity<Void> save(@RequestBody ShopDealer shopDealer) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        long count = shopDealerService.count(new LambdaQueryWrapper<ShopDealer>().eq(ShopDealer::getDealerName, shopDealer.getDealerName()).or().eq(ShopDealer::getDealerCode, shopDealer.getDealerCode()));
        if(count > 0 ){
            throw new YamiShopBindException("已存在同名或同编号经销商信息");
        }
        shopDealer.setShopId(shopId);
        shopDealer.setStatus("1");
        shopDealer.setCreateTime(new Date());
        shopDealer.setUpdateTime(new Date());
        shopDealerService.addShopDealer(shopDealer);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新店铺经销商信息", description = "更新店铺经销商信息")
    public ServerResponseEntity<Void> update(@RequestBody ShopDealer shopDealer) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        long count = shopDealerService.count(new LambdaQueryWrapper<ShopDealer>().eq(ShopDealer::getDealerName, shopDealer.getDealerName()).or().eq(ShopDealer::getDealerCode, shopDealer.getDealerCode()));
        if(count > 0 ){
            throw new YamiShopBindException("已存在同名或同编号经销商信息");
        }
        shopDealer.setShopId(shopId);
        shopDealer.setUpdateTime(new Date());
        shopDealerService.updateShopDealer(shopDealer);
        return ServerResponseEntity.success();
    }

    @GetMapping
    @Operation(summary = "获取店铺经销商信息", description = "获取店铺经销商信息")
    public ServerResponseEntity<ShopDealer> getShopDealerById(@RequestParam(name = "shopDealerId") Integer shopDealerId) {
        ShopDealer shopDealer = shopDealerService.getShopDealerById(shopDealerId);
        if (Objects.isNull(shopDealer)) {
            shopDealer = new ShopDealer();
        }
        return ServerResponseEntity.success(shopDealer);
    }

    @DeleteMapping
    @Operation(summary =  "根据经销商id列表批量删除经销商", description = "根据经销商id列表批量删除经销商")
    public ServerResponseEntity<Void> delete(@RequestBody List<Integer> ids){
        shopDealerService.removeByIds(ids);
        return ServerResponseEntity.success();
    }
}
